Repository Public
1import { get } from "@/server/db";2import { env } from "@/server/env";3import { isProvider, resolveProvider } from "@/server/engine";4import { buildEntries, byToken, cleanFilter } from "@/server/playlists";5import { clientIp } from "@/server/api/core";6import { openRelay } from "@/server/api/relay";7import { eligibility, RelayRefused, streamRow } from "@/server/relay/access";89export const dynamic = "force-dynamic";1011// /m3u/<token>/<streamId>.m3u8 — provider streams listed in a personal playlist. The playlist file only holds this12// address; the provider's own address (with its credentials) is given as a redirect, to a holder of the token, for a13// stream that really belongs to that playlist.14export async function GET(req: Request, ctx: { params: Promise<{ file: string; stream: string }> }) {15 const { file, stream } = await ctx.params;16 const p = byToken(file); const id = Number(/^(\d+)\.m3u8$/.exec(stream)?.[1]);17 if (!p || !id) return new Response("Introuvable\n", { status: 404 });18 if (!buildEntries(cleanFilter(JSON.parse(p.filter))).some((e) => e.streamId === id)) return new Response("Introuvable\n", { status: 404 });19 const s = get<{ url: string }>("SELECT url FROM streams WHERE id = ?", id);20 if (!s || !isProvider(s.url)) return new Response("Introuvable\n", { status: 404 });21 // A stream served by the relay engine (home channels, sources authorised for it): the television gets a temporary22 // session address, opened in the name of this playlist and closed when the playlist token is revoked.23 const row = streamRow(id);24 if (row && eligibility(row).ok) {25 try {26 const r = await openRelay(id, `p:${p.id}`, { ip: clientIp(req), userAgent: req.headers.get("user-agent") });27 return new Response(null, { status: 302, headers: { location: env.appUrl + r.url, "cache-control": "no-store" } });28 } catch (e) { return new Response(`${e instanceof RelayRefused ? e.message : "Relais indisponible"}\n`, { status: e instanceof RelayRefused ? e.status : 503 }); }29 }30 try { return new Response(null, { status: 302, headers: { location: await resolveProvider(s.url), "cache-control": "no-store" } }); }31 catch { return new Response("Fournisseur indisponible\n", { status: 503 }); }32}33